<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.vsg.stock.basic.domain.mybatis.BalanceLineKeyPointProxy">


	<resultMap id="BalanceLineKeypointProxyResult" type="org.vsg.stock.basic.domain.mybatis.BalanceLineKeyPointProxy">
		<id property="pointId" column="POINT_ID" />
		<result property="stockCode" column="STOCK_CODE"/>
		<result property="refDate" column="REF_DATE"/>
		<result property="price" column="PRICE"/>
		<result property="priceType" column="PRICE_TYPE"/>
		<result property="integerFlag" column="INTEGER_FLAG"/>
		<result property="substanceFlag" column="SUBSTANCE_FLAG"/>
	</resultMap>


	<select id="getAveragePriceByLineId" parameterType="map" resultType="map">
  		select avg(p.price) AS avgprice
   			from balance_line_keypoint AS p 
   			inner join balance_line_keypoint_m AS m on p.point_id =  m.point_id
  		where line_id=#{lineId}
 	</select>

	<select id="getDifferenceFromCloselyPriceByLineId" parameterType="map" resultType="map">
		select p2.* , abs(p2.price - (
			select avg(p.price)+${differenceFromPrice} as coun from balance_line_keypoint p
			inner join balance_line_keypoint_m m on p.point_id =  m.point_id
			where line_id = #{lineId}
		)) as ordervalue 
		from balance_line_keypoint p2
		inner join balance_line_keypoint_m m2 on p2.point_id =  m2.point_id
		where line_id = #{lineId} order by ordervalue LIMIT 1  
 	</select>
 	
</mapper>
